<?php
class Builder extends Podcast {

	function __construct() {
		self::$files = $this->getAllFilePath(self::$podcastcache);
	}

	public function generateOutput() {
		// if there is no podcast named, back to home
		(self::$podcast == "") ? header("Location: ".ROOT) : $this->showFeed();
	}

	private function showFeed() {
		// show the modified/original feed or download a file from feed?
		switch (self::$episode) {
			case "":
			case "original":
				$feed = new Feed();
				$feed->showContent(self::$episode == "");
				break;

			default:
				$dl = new Downloader();
				$dl->showContent();
		}
	}

	private function getAllFiles($dir) {
		$res = array();

		$files = glob("$dir/*");

		foreach ($files as $file) {
			if (is_dir($file)) {
				$subfiles = $this->getAllFiles("$file");
				foreach ($subfiles as $subfile) {
					array_push($res, $subfile);
				}
			} else {
				array_push($res, $file);
			}
		}

		return $res;
	}

	private function getAllFilePath($dir) {
		$res = array();
		$files = $this->getAllFiles($dir);

		foreach ($files as $file) {
			$pattern = "#.*/(.*)(\.[ampAMP34]{3})!#";
			if(preg_match($pattern, "$file!", $match)) {
				$res[] = array("filepath" => substr($match[0], 0, -1),
				               "filename" => $match[1],
				               "extension" => $match[2]);
			}
		}

		return $res;
	}
}

?>
